<template>
  <h6>appDome</h6>
</template>

<script lang="ts">
import { customRef, defineComponent } from "vue";
import test from "@/components/day03/test.vue";
// 导入ref
// import { ref } from "vue";
export default defineComponent({
  name: "App",
  // 组合式API入口

  setup() {
    // 定义参数
    // 1 状态的初始值,防抖的延迟时间
    const test = useDebounceRef("hello", 400);

    function useDebounceRef(initialValue: any, delay: any) {
      let tiemr: any = null;
      //创建响应式数据并返回
      // customRef 需要调用
      return customRef((track, trigger) => {
        return {
          // 获取响应式数据的时候调用
          get() {
            track();
            return initialValue;
          },
          // 设置响应式数据调用
          set(newValue) {
            timer = setTimeout(() => {
              clearTimeout(timer);
              // newvalue 为设置的新值
              initialValue = newValue;
              trigger();
            }, 400);
          },
        };
      });
    }
    // 返回值
    return { test };
  },
  components: {},
});
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>
